/**
 * 
 */
package examples.client;

import java.util.Properties;

import javax.ejb.NoSuchEJBException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import examples.session.stateful.Cart;

/**
 * @author 814168
 * 
 */
public class CartClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Properties jndiProps = new Properties();
			jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			jndiProps.setProperty(Context.URL_PKG_PREFIXES,
					"org.jboss.naming:org.jnp.interface");
			jndiProps.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");

			InitialContext ic = new InitialContext(jndiProps);

			for (int i = 0; i < 2; i++) {
				Cart cart = (Cart) ic.lookup("CartBean/remote");
				cart.addItem();
				cart.addItem();
				System.out.println("items in the cart " + i + ": " + cart.getItems());
				try {
					try {
						if (i == 0) {
							cart.remove1();
						} else {
							cart.remove2();
						}
					} catch (Exception e) {
						;
					}
					cart.addItem();
					cart.addItem();
					cart.addItem();
					System.out.println("END: items in the cart " + i + ": " + cart.getItems());
				} catch (NoSuchEJBException esoe) {
					System.out.println("Cart was already removed "
							+ "during iteration " + i);
				}
			}
		} catch (NamingException e) {
			e.printStackTrace();
		}

	}

}
